#include <iostream>//intArray.cpp
#include "intArray.h"

using namespace std;

intArray::intArray(int size = 0)
{
    arraysize=size;
    element=new int[size];
}

intArray::intArray(const intArray& x)
{
    arraysize=x.arraysize;
    int *p = new int;
    for(int i=0;i<arraysize;i++)
        p[i]=x.element[i];
    element=p;
}

intArray::~intArray()
{
    arraysize=0;
    delete []element;
}

bool intArray::Set(int i,int elem)
{
    element[i]=elem;
    if(element[i]==elem)
        return true;
    else
        return false;
}

bool intArray::Get(int i,int& elem)
{
    elem = element[i];
    if(elem==element[i])
        return true;
    else
        return false;
}

int intArray::Length() const
{
    return arraysize;
}

void intArray::ReSize(int size)
{
    delete []element;
    arraysize = size;
    element = new int[size];
}

intArray intArray::operator=(const intArray& other)
{
    arraysize=other.arraysize;
    for(int i=0;i<other.arraysize;i++)
    {
        element[i]=other.element[i];
    }
}

intArray intArray::operator+(const intArray& other)
{
    intArray arr;
    arr.arraysize=other.arraysize;
    int i;
    for(i=0;i<other.arraysize;i++)
    {
        arr.element[i]=element[i]+other.element[i];
    }
    return arr;
}

intArray intArray::operator-(const intArray& other)
{
    intArray arr;
    arr.arraysize=other.arraysize;
    int i;
    for(i=0;i<other.arraysize;i++)
    {
        arr.element[i]=element[i]-other.element[i];
    }
    return arr;
}

istream & operator>>(istream& input,intArray& arr)
{
    int length;
    cout<<"Please input the length of array: ";
    input>>length;
    if(length != arr.Length())
    {
        arr.ReSize(length);
    }
    int element;
    cout<<"then input the content: ";
    for(int i=0;i<length;i++)
    {
        input>>element;
        arr.Set(i,element);
    }
    cout<<endl;
    return input;
}

ostream & operator<<(ostream& output,intArray& arr)
{
    cout<<"the length of the array: ";
    output<<arr.arraysize;
    cout<<endl<<"the content: ";
    for(int i=0;i<arr.arraysize;i++)
    {
        output<<arr.element[i];
        cout<<"  ";
    }
    cout<<endl<<endl;
    return output;
}
